<template>
  <div class="wrapper">
    <div class="uc-main clearfix">
      <div class="uc-aside">
        <user-menu></user-menu>
      </div>
      <div class="uc-content">
        <div class="uc-panel">
          <div class="uc-bigtit">类别列表</div>
          <div class="uc-panel-bd">
            <el-table
              :data="tableData"
              style="width: 100%">
              <el-table-column
                label="类别名称"
                prop="categoryName">
              </el-table-column>
              <el-table-column
                align="right">
                <template slot="header" slot-scope="scope">
                  <el-button
                    size="mini"
                    @click="openFrom()">新增</el-button>
                </template>
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="类别" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="类别名称" :label-width="formLabelWidth">
          <el-input v-model="form.categoryName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCategory()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import UserMenu from './UserMenu'
import Util from '../../../common/util'
import http from '../../../base/api/http'
export default {
  name: 'AdminCategoryWrapper',
  components: {
    UserMenu
  },
  data () {
    return {
      form: {
        categoryId: '',
        categoryName: ''
      },
      tableData: [],
      search: '',
      myHeaders: {
        Authorization: Util.getToken()
      },
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  methods: {
    handleEdit (index, row) {
      this.openFrom(row.categoryId)
    },
    handleDelete (index, row) {
      this.open2(row.categoryId)
    },
    openFrom (id) {
      if (id !== undefined && id !== '') {
        this.getCategory(id)
      }
      this.dialogFormVisible = true
    },
    getCategory (id) {
      http.requestGet('/java/api/category/findByCategoryId', {
        'categoryId': id
      }).then((res) => {
        if (res.status === 0) {
          this.form = res.data
        }
      })
    },
    saveCategory () {
      http.requestPost('/java/api/category/saveCategory', this.form).then((res) => {
        if (res.status === 0) {
          alert('操作成功')
          this.$router.go(0)
        } else {
          alert('操作失败')
        }
      })
      this.dialogFormVisible = false
    },
    deleteCategoryId (id) {
      http.requestGet('/java/api/category/deleteCategory', {
        'categoryId': id
      }).then((res) => {
        if (res.status === 0) {
          alert('删除成功')
          this.$router.go(0)
        } else {
          alert('删除失败')
        }
      })
    },
    open2 (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteCategoryId(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    findAll () {
      http.requestQuickGet('/java/api/category/findAll').then((res) => {
        if (res.status === 0) {
          this.tableData = res.data
        }
      })
    }
  },
  mounted () {
    this.findAll()
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
</style>
